package cn.iocoder.yudao.module.promotion.activity.controller.common;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.api.promotion.activity.enums.PromotionTypeEnum;
import cn.iocoder.yudao.module.promotion.activity.convert.ActivityConvert;
import cn.iocoder.yudao.module.promotion.activity.vo.ActivityVO;
import cn.iocoder.yudao.module.promotion.bargain.dal.dataobject.BargainActivityDO;
import cn.iocoder.yudao.module.promotion.bargain.service.BargainActivityService;
import cn.iocoder.yudao.module.promotion.combination.dal.dataobject.CombinationActivityDO;
import cn.iocoder.yudao.module.promotion.combination.service.CombinationActivityService;
import cn.iocoder.yudao.module.promotion.seckill.dal.dataobject.SeckillActivityDO;
import cn.iocoder.yudao.module.promotion.seckill.service.SeckillActivityService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import jakarta.annotation.Resource;
import jakarta.annotation.security.PermitAll;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

public abstract class ActivityBaseController {

    @Resource
    private CombinationActivityService combinationActivityService;
    @Resource
    private SeckillActivityService seckillActivityService;
    @Resource
    private BargainActivityService bargainActivityService;

    @GetMapping("/list-by-spu-id")
    @Operation(summary = "获得单个产品进行中的活动信息", description = "拼团、秒杀、砍价，每种活动只返回一个")
    @Parameter(name = "spuId", description = "产品编号", required = true)
    @PermitAll
    public CommonResult<List<ActivityVO>> getActivityListBySpuId(@RequestParam("spuId") Long spuId) {
        List<ActivityVO> activityList = new ArrayList<>();
        // 1. 拼团活动
        CombinationActivityDO combinationActivity = combinationActivityService.getMatchCombinationActivityBySpuId(spuId);
        if (combinationActivity != null) {
            ActivityVO activityRespVO = ActivityConvert.INSTANCE.convert(combinationActivity, PromotionTypeEnum.COMBINATION_ACTIVITY.getValue());
            activityList.add(activityRespVO);
        }
        // 2. 秒杀活动
        SeckillActivityDO seckillActivity = seckillActivityService.getMatchSeckillActivityBySpuId(spuId);
        if (seckillActivity != null) {
            ActivityVO activityRespVO = ActivityConvert.INSTANCE.convert(seckillActivity, PromotionTypeEnum.SECKILL_ACTIVITY.getValue());
            activityList.add(activityRespVO);
        }
        // 3. 砍价活动
        BargainActivityDO bargainActivity = bargainActivityService.getMatchBargainActivityBySpuId(spuId);
        if (bargainActivity != null) {
            ActivityVO activityRespVO = ActivityConvert.INSTANCE.convert(bargainActivity, PromotionTypeEnum.BARGAIN_ACTIVITY.getValue());
            activityList.add(activityRespVO);
        }
        return success(activityList);
    }

}
